package com.project.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.project.bean.CommentBean;
import com.project.bean.PageBean;
import com.project.bean.StudentBean;
import com.project.dao.CommentMapper;
import com.project.service.ICommentService;
import com.project.util.DateUtil;

@Service(value = "commentService")
public class CommentServiceImpl implements ICommentService {

	@Autowired
	private CommentMapper commentMapper;
	@Override
	public int addComment(CommentBean comment) {
		// TODO Auto-generated method stub
		return commentMapper.insert(comment);
	}

	@Override
	public int deleteComment(Integer id) {
		// TODO Auto-generated method stub
		return commentMapper.deleteByPrimaryKey(id);
	}

	@Override
	public PageBean showCommentsByPage(Integer pid, Integer page, Integer size) {
		PageBean pageBean=new PageBean();
		Map<String,Integer> map=new HashMap<String,Integer>();
		map.put("page", (page-1)*size);
		map.put("size", size);
		map.put("pid", pid);
		List<CommentBean> list=commentMapper.findByPage(map);
		for (CommentBean commentBean : list) {
			Date date=commentBean.getCmTime();
			String time=DateUtil.changeTime(date);
			commentBean.setCmDate(time);		
		}
		int num=commentMapper.findAllByPid(pid).size();
		int totalpage=(num%size==0)?(num/size):(num/size+1);
		pageBean.setPage(page);
		pageBean.setSize(size);
		pageBean.setTotalNumber(num);
		pageBean.setTotalPage(totalpage);
		pageBean.setList(list);
		return pageBean;

	}

	@Override
	public List<CommentBean> findCommentsByPost(Integer pid) {
		// TODO Auto-generated method stub
		return commentMapper.findAllByPid(pid);
	}

}
